# import time
#
# from selenium import webdriver
# from selenium.webdriver.chrome.service import Service as ChromeService
# from selenium.webdriver.common.by import By
# from webdriver_manager.chrome import ChromeDriverManager
#
# # 打开浏览器
# driver = webdriver.Chrome(service = ChromeService(ChromeDriverManager().install()))
# time.sleep(1)
# # 输入网址
# driver.get("https://www.baidu.com")
# time.sleep(1)
#
# # 找到输⼊框并输⼊“迪丽热巴”
# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("迪丽热巴")
# time.sleep(1)
#
# # 找到“百度⼀下”按钮并点击
# driver.find_element(By.CSS_SELECTOR,"#su").click()
# time.sleep(1)
#
# # 关闭浏览器
# driver.quit()

# import time
# from selenium import webdriver
# from selenium.webdriver.common.by import By
# from selenium.webdriver.edge.service import Service as EdgeService
# from webdriver_manager.microsoft import EdgeChromiumDriverManager
#
# # 1. 使用 EdgeChromiumDriverManager 自动下载并安装 EdgeDriver
# edge_service = EdgeService(EdgeChromiumDriverManager().install())
#
# # 2. 创建 Edge 浏览器驱动对象
# driver = webdriver.Edge(service=edge_service)
# time.sleep(1)
# # 3. 访问百度
# driver.get("https://www.baidu.com/")
# time.sleep(1)
#
# # 4. 在搜索框输入 "迪丽热巴"
# driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("迪丽热巴")
# time.sleep(1)
#
# # 5. 点击 "百度一下" 按钮
# driver.find_element(By.CSS_SELECTOR, "#su").click()
# time.sleep(1)
#
# # 6. 关闭浏览器
# driver.quit()

# import time
# from selenium.webdriver.common.by import By
# from common.Utils import BlogDriver


# class BlogList:
#     driver = ""
#     url = ""
#
#     def __init__(self):
#         self.driver = BlogDriver.driver
#         self.url = "http://8.137.19.140:9090/blog_list.html"
#         self.driver.get(self.url)
#
#     def BlogLoginListTest(self):  # 登路状态下
#         # 检查一下博客的标题是否存在
#         self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > div.title")
#         # 检查一下发布博客的时间是否存在
#         self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > div.date")
#         # 检查一下博客的内容是否存在
#         self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > div.desc")
#         # 检查一下博客列表页的查看全文按钮是否存在
#         self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > a")
#         # 检查一下用户基本信息的昵称是否存在
#         self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")
#         # 检查一下用户基本信息的文章是否存在
#         self.driver.find_element(By.CSS_SELECTOR,
#                                  "body > div.container > div.left > div > div:nth-child(4) > span:nth-child(1)")
#         # 检查一下用户基本信息的分类是否存在
#         self.driver.find_element(By.CSS_SELECTOR,
#                                  "body > div.container > div.left > div > div:nth-child(4) > span:nth-child(2)")
#         # 检查一下博客数量是否为0，不为就就通过
#         Blognum = self.driver.find_element(By.CSS_SELECTOR,
#                                            "body > div.container > div.left > div > div:nth-child(5) > span:nth-child(1)").text
#         print(Blognum)
#         assert Blognum != 0
#         BlogDriver.GetScreeShot()
#         # self.driver.quit()
#
#     def NotBlogLoginListTest(self):  # 未登录状态下
#         # # 找到用户命名输入框和密码输入框把里面的内容清空
#         # self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
#         # self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
#         self.driver.get("http://8.137.19.140:9090/blog_list.html")
#         time.sleep(2)
#         BlogDriver.GetScreeShot()
#         self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > div.title")
#
#     def QuitBlogLoginListText(self):  # 登录博客状态下测试退出
#         # 点击退出按钮
#         self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(6)").click()
#         BlogDriver.GetScreeShot()
#         # 判断一下退出后的页面url和页面的标题
#         Quittitle = self.driver.title
#         QuitUrl = self.driver.current_url
#         print(Quittitle)
#         print(QuitUrl)
#         assert Quittitle == "博客登陆页"
#         assert QuitUrl == "http://8.137.19.140:9090/blog_login.html"
#         # 判断一下账号输入框和密码输入框的内容是否为空
#         Quitusername = self.driver.find_element(By.CSS_SELECTOR, "#username").text
#         Quitpassword = self.driver.find_element(By.CSS_SELECTOR, "#password").text
#         print("Quitusername" + Quitusername)
#         print("Quitpassword" + Quitpassword)
#         assert Quitusername == ""
#         assert Quitpassword == ""

# import os
# import sys
# import datetime
# from selenium import webdriver
# # from selenium.webdriver.support.wait import WebDriverWait
# from webdriver_manager.microsoft import EdgeChromiumDriverManager
# from selenium.webdriver.edge.service import Service
#
#
# class Driver:
#     driver = ""
#
#     def __init__(self):
#         options = webdriver.EdgeOptions()
#         self.driver = webdriver.Edge(service=Service(EdgeChromiumDriverManager().install()), options=options)
#         # 添加一个隐式等待，预防代码执行速度比页面渲染加载快，报错
#         self.driver.implicitly_wait(2)  # 隐式等待两秒，隐式等待的生命周期是整个查找过程
#
#     # 截图函数
#     def GetScreeShot(self):
#         # 首先在上一级路径创建一个文件夹，命名为：images+当天的年月日
#         # 创建之前先看一下，文件夹是否存在
#         driname = datetime.datetime.now().strftime("%Y-%m-%d")
#         if not os.path.exists("../images/" + driname):
#             # 如果不存在，就创建文件夹
#             os.mkdir("../images/" + driname)
#         # 截图的图片名称要加上调用是哪个函数调用这个截图函数的
#         # 图片名称：BlogLoginTest-2025-3-23-123409
#         filename = sys._getframe().f_back.f_code.co_name + "-" + datetime.datetime.now().strftime(
#             "%Y-%m-%d-%H%M%S") + ".png"
#         # 利用驱动对象调用截图函数
#         self.driver.save_screenshot("../images/" + driname + "/" + filename)
#
#
# BlogDriver = Driver()  # 使用Driver类创建一个类驱动对象，其他函数想要使用这个驱动就得使用BlogDriver调用里面的驱动成员driver

import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from common.Utils import BlogDriver


# class BlogDetail:
#     driver = ""
#     url = ""
#
#     def __init__(self):
#         self.driver = BlogDriver.driver
#         # 一篇博客的url
#         self.url = "http://8.137.19.140:9090/blog_detail.html?blogId=38068"
#         self.driver.get(self.url)
#
#     def BlogLoginDetailTest(self):
#         # 检查一下博客的标题是否存在
#         self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div > div.title")
#         # 检查一下发布博客的时间是否存在
#         self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div > div.date")
#         # 检查一下博客的内容是否存在
#         self.driver.find_element(By.CSS_SELECTOR, "#detail")
#         # 检查一下用户基本信息的昵称是否存在
#         self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")
#         # 检查一下用户基本信息的文章是否存在
#         self.driver.find_element(By.CSS_SELECTOR,
#                                  "body > div.container > div.left > div > div:nth-child(4) > span:nth-child(1)")
#         # 检查一下用户基本信息的分类是否存在
#         self.driver.find_element(By.CSS_SELECTOR,
#                                  "body > div.container > div.left > div > div:nth-child(4) > span:nth-child(2)")
#         # 检查一下编辑按钮是否存在
#         self.driver.find_element(By.CSS_SELECTOR,
#                                  "body > div.container > div.right > div > div.operating > button:nth-child(1)")
#         # 检查一下删除按钮是否存在
#         self.driver.find_element(By.CSS_SELECTOR,
#                                  "body > div.container > div.right > div > div.operating > button:nth-child(2)")
#         # 添加屏幕截图
#         BlogDriver.GetScreeShot()
#         time.sleep(2)
#
#     def DeleteBlogLoginDetailTest(self):
#         # 点击第一篇博客删除按钮
#         BlogDriver.GetScreeShot()
#         self.driver.find_element(By.CSS_SELECTOR,
#                                  "body > div.container > div.right > div > div.operating > button:nth-child(2)").click()
#         # 点击删除按钮后会出现一个弹窗
#         wait = WebDriverWait(self.driver, 2)
#         # 检查弹窗是否出现
#         wait.until(EC.alert_is_present())
#         # 找到弹窗
#         alert = self.driver.switch_to.alert
#         # 判断一下弹窗里面的内容是否是确定删除？
#         actual = alert.text
#         print(actual)
#         assert actual == "确定删除?"
#         # 点击确定删除按钮
#         alert.accept()
#         time.sleep(1)
#         BlogDriver.GetScreeShot()
#         # 删除后判断一下是否返回到了博客列表页
#         time.sleep(1)
#         rettitle = self.driver.title
#         retUrl = self.driver.current_url
#         assert rettitle == "博客列表页"
#         assert retUrl == "http://8.137.19.140:9090/blog_list.html"
#         print("rettitle:" + rettitle)
#         print("retUrl:" + retUrl)
#
#     def NotBlogLoginDetailTest(self):
#         # 检查一下博客的标题是否存在
#         self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div > div.title")
#         # 检查一下发布博客的时间是否存在
#         self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div > div.date")
#         # 检查一下博客的内容是否存在
#         self.driver.find_element(By.CSS_SELECTOR, "#detail")
#         # 检查一下用户基本信息的昵称是否存在
#         self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")
#         # 检查一下用户基本信息的文章是否存在
#         self.driver.find_element(By.CSS_SELECTOR,
#                                  "body > div.container > div.left > div > div:nth-child(4) > span:nth-child(1)")
#         # 检查一下用户基本信息的分类是否存在
#         self.driver.find_element(By.CSS_SELECTOR,
#                                  "body > div.container > div.left > div > div:nth-child(4) > span:nth-child(2)")
#         # 检查一下编辑按钮是否存在
#         self.driver.find_element(By.CSS_SELECTOR,
#                                  "body > div.container > div.right > div > div.operating > button:nth-child(1)")
#         # 检查一下删除按钮是否存在
#         self.driver.find_element(By.CSS_SELECTOR,
#                                  "body > div.container > div.right > div > div.operating > button:nth-child(2)")
#         # 添加屏幕截图
#         BlogDriver.GetScreeShot()
#         time.sleep(2)

import time
from selenium.webdriver.common.by import By
from common.Utils import BlogDriver


class BlogEdit:
    driver = ""
    url = ""

    def __init__(self):
        self.driver = BlogDriver.driver
        # 编辑一篇新博克页面
        self.url = "http://8.137.19.140:9090/blog_edit.html"
        self.driver.get(self.url)

    def BlogLoginEditTest(self):
        # 找到标题输入框输入标题
        self.driver.find_element(By.CSS_SELECTOR, "#title").send_keys("博客系统测试编辑页面")
        # 编辑区域的菜单栏是第三方插件，元素无法被选中，先不处理菜单栏
        # 博客编辑区本来就不为空
        time.sleep(1)
        BlogDriver.GetScreeShot()
        # 点击发布文章按钮
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 因为页面的跳转需要时间，代码执行的速度如果比页面渲染的速度快，就会导致找不到元素
        time.sleep(1)
        # 发布文章后，会自动跳转到博客列表页，可以判断一下跳转后的url是否等于博客列表页的url
        jumpURL = self.driver.current_url
        print(jumpURL)
        assert jumpURL == "http://8.137.19.140:9090/blog_list.html"
        # 判断一下博客列表页的文章标题是否是新发布的博客的给定的标题
        # body > div.container > div.right > div: nth - child(33) > div.title，最近发布一篇文章是child（33）新发布的文章应该是child（34）
        actual = self.driver.find_element(By.CSS_SELECTOR,
                                          "body > div.container > div.right > div:nth-child(34) > div.title").text
        print(actual)
        assert actual == "博客系统测试编辑页面"

    def NotBlogLoginEditTest(self):  # 未登录下
        # 未登录下是找不到标题输入框输入标题
        self.driver.find_element(By.CSS_SELECTOR, "#title").send_keys("博客系统测试编辑页面")